﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using System.ComponentModel;
using BusinessPlatform.Data;

namespace BusinessPlatform.Common
{
    public static class ConvertEx
    {
        /// <summary>
        /// .NET类型转换为DbType
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DbType ToDbType(Type type)
        {
            if (type.IsEnum)
                return DbType.String;
            return (DbType)Enum.Parse(typeof(DbType), type.Name);
        }
        public static Type ToSystemType(DbType dbType)
        {
            Type type = default(Type);
            switch (dbType)
            {
                case DbType.Int16:
                    type = typeof(System.Int16);
                    break;
                case DbType.Int32:
                    type = typeof(System.Int32);
                    break;
                case DbType.Int64:
                    type = typeof(System.Int64);
                    break;
                case DbType.Decimal:
                    type = typeof(System.Decimal);
                    break;
                case DbType.String:
                case DbType.StringFixedLength:
                    type = typeof(System.String);
                    break;
                case DbType.Boolean:
                    type = typeof(System.Boolean);
                    break;
                case DbType.Byte:
                    type = typeof(System.Byte);
                    break;
                case DbType.Double:
                    type = typeof(System.Double);
                    break;
                case DbType.Single:
                    type = typeof(System.Single);
                    break;
                case DbType.Date:
                case DbType.Time:
                case DbType.DateTime:
                case DbType.DateTime2:
                    type = typeof(System.DateTime);
                    break;
                case DbType.DateTimeOffset:
                    type = typeof(System.DateTimeOffset);
                    break;
                case DbType.Binary:
                    type = typeof(System.Byte[]);
                    break;
                case DbType.Guid:
                    type = typeof(System.Guid);
                    break;
                case DbType.Object:
                    type = typeof(System.Object);
                    break;
            }
            return type;
        }
        /// <summary>
        /// 获取枚举类型的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="EnumValue"></param>
        /// <returns></returns>
        public static string GetEnumValue<T>(T enumValue)
        {
            Type type = enumValue.GetType();
            FieldInfo info = type.GetField(enumValue.ToString());
            if (info == null) return null;
            return (info.GetCustomAttributes(typeof(EnumValueAttribute), true)[0] as EnumValueAttribute).Value.ToString();
        }
        /// <summary>
        /// 权限字段值获取枚举值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static T GetEnumValue<T>(string Value)
        {
            Type type = typeof(T);
            foreach (var field in type.GetFields())
            {
                if (field.FieldType == typeof(T))
                    if ((field.GetCustomAttributes(typeof(EnumValueAttribute), true)[0] as EnumValueAttribute).Value.ToString() == Value)
                        return (T)Enum.Parse(typeof(T), field.Name.ToString());
            }
            return default(T);
        }
        /// <summary>
        /// 获取枚举类型的值
        /// </summary>
        /// <param name="EnumValue"></param>
        /// <returns></returns>
        public static string GetEnumValue(object EnumValue)
        {
            Type type = EnumValue.GetType();
            FieldInfo info = type.GetField(EnumValue.ToString());
            if (info == null) return null;
            return (info.GetCustomAttributes(typeof(EnumValueAttribute), true)[0] as EnumValueAttribute).Value.ToString();
        }
        public static object GetEnum(Type EnumType, string Value)
        {
            Type type = EnumType;
            foreach (var field in type.GetFields())
            {               
                if (field.FieldType == EnumType)
                {
                    EnumValueAttribute[] list = (EnumValueAttribute[])field.GetCustomAttributes(typeof(EnumValueAttribute), false);
                    if(list.Length > 0)                    
                    if (list[0].Value.ToString() == Value)
                        return Enum.Parse(EnumType, field.Name.ToString(), false);
                }
            }
            return null;
        }
    }
}
